<?php
class ProjectAssignsController extends AppController {

	var $name = 'ProjectAssigns';
	var $paginate = array(
				'limit' => 10,
				'order' => array(
				'ProjectAssign.project_year_id' => 'asc',
				'ProjectAssign.project_category_id' => 'asc',
				'ProjectAssign.user_id' => 'asc',
				'ProjectAssign.number' => 'asc'
				
				)
				); 

	function index($project_year_id = null) {
		$this->ProjectAssign->recursive = 0;
		if(!empty($this->data)){
			$project_year = '';
			$project_category ='';
			$project_teacher = '';
			if(!empty($this->data['project_year'])){
				$project_year = $this->data['project_year'];
				$this->set('set_year',$project_year);
			}
			//1 Lĩnh vực 	=> Không được chọn
			//  Giáo viên 	=> Không được chọn
			if(empty($this->data['project_category']) && empty($this->data['project_teacher'])){
				$this->Session->setFlash(__('Hãy chọn một lĩnh vực / Giáo viên để xem thông tin', true));
				$this->redirect(array('action' => 'index'));
			}
			//1 Lĩnh vực 	=> Được chọn
			//  Giáo viên 	=> Không được chọn
			else if(!empty($this->data['project_category']) && empty($this->data['project_teacher'])){
				$this->paginate['conditions'] = array('ProjectAssign.project_year_id'=>$project_year,'ProjectAssign.project_category_id'=>$this->data['project_category']);
				//Set choose option
				$this->set('set_year',$project_year);
				$this->set('set_category',$this->data['project_category']);
				$this->set('set_teacher',$project_teacher);
				$this->Session->setFlash(__('Xem thông tin theo lĩnh vực đăng ký', true));
			}
			//1 Lĩnh vực 	=> Không được chọn
			//  Giáo viên 	=> Được chọn
			else if(empty($this->data['project_category']) && !empty($this->data['project_teacher'])){
				$this->paginate['conditions'] = array('ProjectAssign.project_year_id'=>$project_year,'ProjectAssign.user_id'=>$this->data['project_teacher']);
				//Set choose option
				$this->set('set_year',$project_year);
				$this->set('set_category',$project_category);
				$this->set('set_teacher',$this->data['project_teacher']);
				$this->Session->setFlash(__('Xem thông tin theo Giáo viên hướng dẫn', true));
			}
			else if(!empty($this->data['project_category']) && !empty($this->data['project_teacher'])){
				$this->paginate['conditions'] = array('ProjectAssign.project_year_id'=>$project_year,'ProjectAssign.project_category_id'=>$this->data['project_category'],'ProjectAssign.user_id'=>$this->data['project_teacher']);
				//Set choose option
				$this->set('set_year',$project_year);
				$this->set('set_category',$this->data['project_category']);
				$this->set('set_teacher',$this->data['project_teacher']);
				$this->Session->setFlash(__('Xem thông tin theo Lĩnh vực và Giáo viên hướng dẫn', true));
			}

		}
		//Nếu list index
		else{
			$get_project_active = $this->ProjectAssign->ProjectYear->find('all',array('conditions'=>array('ProjectYear.active'=>1)));
			$get_one_ProjectCategory = $this->ProjectAssign->ProjectCategory->find('first',array('order'=>array('ProjectCategory.created asc')));
			$this->paginate['conditions'] = array('ProjectAssign.project_year_id'=>$get_project_active[0]['ProjectYear']['id'],'ProjectAssign.project_category_id'=>$get_one_ProjectCategory['ProjectCategory']['id']);
			$this->set('set_category',$get_one_ProjectCategory['ProjectCategory']['id']);
			$this->set('set_year',$get_project_active[0]['ProjectYear']['id']);
		}
		//List all Option
		$this->set('projectAssigns',$this->paginate());
		$this->set('set_project_year',$this->ProjectAssign->ProjectYear->find('list'));
		$this->set('set_teachers', $this->ProjectAssign->User->find('list',array('conditions'=>array('User.group_id'=>2))));
		$this->set('set_project_category',$this->ProjectAssign->ProjectCategory->find('list'));
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid project assign', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('projectAssign', $this->ProjectAssign->read(null, $id));
	}

	function add($project_category_id =null) {
		if (!empty($this->data)) {
			$this->ProjectAssign->create();
			$get_results = $this->ProjectAssign->find('all',array('conditions'=>array('ProjectAssign.project_year_id'=>$this->data['ProjectAssign']['project_year_id'],'ProjectAssign.project_category_id'=>$this->data['ProjectAssign']['project_category_id'],'ProjectAssign.user_id'=>$this->data['ProjectAssign']['user_id'])));
			if(count($get_results) >0){
				$this->Session->setFlash(__('Năm này Giáo viên này đã được phân công', true));
				$this->redirect(array('action' => 'index'));
			}
			if ($this->ProjectAssign->save($this->data)) {
				$this->Session->setFlash(__('Phân công giáo viên hướng dẫn thành công', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Chưa thực hiện được. Hãy kiểm tra lại', true));
			}
		}
		if($project_category_id != null)
			$projectCategories = $this->ProjectAssign->ProjectCategory->find('list',array('conditions'=>array('ProjectCategory.id'=>$project_category_id)));
		else
			$projectCategories = $this->ProjectAssign->ProjectCategory->find('list');
		$projectYears = $this->ProjectAssign->ProjectYear->find('list',array('conditions'=>array('ProjectYear.active'=>1)));
		$users = $this->ProjectAssign->User->find('list',array('conditions'=>array('User.group_id'=>2)));
		$this->set(compact('projectYears', 'projectCategories', 'users'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid project assign', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			$get_results = $this->ProjectAssign->find('all',array('conditions'=>array('ProjectAssign.project_year_id'=>$this->data['ProjectAssign']['project_year_id'],'ProjectAssign.project_category_id'=>$this->data['ProjectAssign']['project_category_id'],'ProjectAssign.user_id'=>$this->data['ProjectAssign']['user_id'])));
			if(count($get_results) >1){
				$this->Session->setFlash(__('Năm này Giáo viên này đã được phân công', true));
				$this->redirect(array('action' => 'index'));
			}
			
			if ($this->ProjectAssign->save($this->data)) {
				$this->Session->setFlash(__('The project assign has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The project assign could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->ProjectAssign->read(null, $id);
		}
		$projectYears = $this->ProjectAssign->ProjectYear->find('list',array('conditions'=>array('ProjectYear.active'=>1)));
		$projectCategories = $this->ProjectAssign->ProjectCategory->find('list');
		$users = $this->ProjectAssign->User->find('list',array('conditions'=>array('User.group_id'=>2)));
		$this->set(compact('projectYears', 'projectCategories', 'users'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for project assign', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->ProjectAssign->delete($id)) {
			$this->Session->setFlash(__('Project assign deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Project assign was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
?>